<?php
namespace app\api\controller;

class Order extends Base
{

    public function _initialize()
    {
        parent::_initialize();
        //$this->chkToken();
        $post['username'] = $this->username;
        $post['password'] = $this->password;
        $data = $this->sendInfo('userLogin', $post);
        if ($data['code'] != 1) {
            $return['success'] = false;
            $return['desc'] = $data['msg'] . '，请联系优士创新';
            $this->ajaxReturn($return);
        }
        if ($data['data']['member_id'] != $this->member_id) {
            $return['success'] = false;
            $return['desc'] = '会员账号信息错误，请联系优士创新';
            $this->ajaxReturn($return);
        }
    }

    public function submit()
    {
        // sku 提交过来的是字符串 需要转成json数组
        $sku_str = $this->request['sku'];
        $sku_str = str_replace("]\"", "]", str_replace("\"[", "[", $sku_str));
        $sku = json_decode($sku_str,true);
        $cart_info = [];
        foreach ($sku as $key => $val) {
            $temp['goods_sku'] = $val['sku'];
            $temp['num'] = $val['num'];
            $temp['goods_num'] = $val['num'];
            $temp['member_id'] = $this->member_id;
            $temp['set_sale_price'] = 'api_price';
            $temp['sale_attr'] = '';
            $temp['sale_attr_html'] = '';
            $temp['goods_sale_price'] = $val['price'];
            $temp['goods_market_price'] = $val['price'];
            $temp['goods_price'] = $val['price'];
            $post_goods['goods_sku'] = $val['sku'];
            $post_goods['member_id'] = $this->member_id;
            $temp['is_install'] = 2;
            
            /*
            $get_price = $this->sendInfo('goodsOnlyDetail', $post_goods);
            if(!isset($get_price['data']['api_price'])){
                $return['success'] = false;
                $return['desc'] = '商品：' . $val['sku'] . '已下架';
                $this->ajaxReturn($return);
            }
            if ($val['price'] != $get_price['data']['api_price']) {
                $return['success'] = false;
                $return['desc'] = '商品：' . $val['sku'] . '价格不正确';
                $this->ajaxReturn($return);
            }
            */
            array_push($cart_info, $temp);
        }
        $post_cart['cart_info'] = $cart_info;
        $post_cart['set_sale_price'] = 'yes';
        $post_cart['no_need_goods_handle'] = 'yes';
        $post_cart['no_need_attr'] = 'yes';
        $sql = $this->sendInfo('addCartArr', $post_cart);
        
        // 添加收货地址
        $post_addr['field']['province'] = '120000';
        $post_addr['field']['city'] = '120100';
        $post_addr['field']['area'] = '120101';
        if (isset($this->request['area'])) {
            $post_addr['field']['area'] = $this->request['area'];
        }
        $post_addr['field']['street'] = $this->request['address'];
        $post_addr['field']['contacts'] = $this->request['name'];
        if ($this->request['mobile']) {
            $post_addr['field']['phone'] = $this->request['mobile'];
        } else {
            $post_addr['field']['phone'] = $this->request['phone'];
        }
        $post_addr['member_id'] = $this->member_id;
        $post_addr['field']['is_default'] = 'on';
        $address_id = $this->sendInfo('saveApiAddress', $post_addr);
        // 添加订单
        $post_order['member_id'] = $this->member_id;
        $post_order['order_info']['member_id'] = $this->member_id;
        $post_order['order_info']['pay_type'] = 'zhangqi';
        $post_order['order_info']['out_order_no'] = $this->request['yggc_order'];
        $post_order['order_info']['order_state'] = 1;
        $post_order['order_info']['shopping_id'] = $address_id;
        $post_order['order_info']['remark'] = $this->request['remark'];
        $post_order['order_info']['api_param'] = json_encode($this->request);
        $post_order['order_info']['is_invoice'] = 2;
        $post_order['order_info']['invoice_state'] = 1;
        $post_order['order_info']['shopping_state'] = 1;
        $post_order['order_info']['order_money'] = $this->request['order_price']*100;
        // 添加发票信息
        $post_order['order_info']['invoice_title'] = $this->request['invoice_title'];
        if ($this->request['invoice_type'] == 2) {
            $post_order['order_info']['invoice_type'] = 3;
        } elseif ($this->request['invoice_type'] == 3) {
            $post_order['order_info']['invoice_type'] = 2;
        } else {
            $post_order['order_info']['invoice_type'] = 1;
        }
        $post_order['order_info']['invoice_duty'] = $this->request['invoice_org_code'];
        $post_order['order_info']['invoice_addr'] = $this->request['invoice_address'];
        $post_order['order_info']['invoice_tel'] = $this->request['invoice_phone'];
        $post_order['order_info']['invoice_bank'] = $this->request['invoice_bank'];
        $post_order['order_info']['invoice_account'] = $this->request['invoice_bank_code'];
        $post_order['order_info']['invoice_money'] = $this->request['order_price'];
        $get_order = $this->sendInfo('createOrder', $post_order);
        
        // 发票结束
        $return['success'] = true;
        $return['result']['mall_order_id'] = $get_order['data'];
        $return['result']['sku'] = $this->request['sku'];
        $return['result']['orderPrice'] = $this->request['order_price'];
        $return['result']['freight'] = 0;
        $this->ajaxReturn($return);
    }
    // 确认订单
    public function confirm()
    {
        $post['order_no'] = $this->request['order_id'];
        $post['member_id'] = $this->member_id;
        $this->sendInfo('confirmOrder', $post);
        $return['success'] = true;
        $this->ajaxReturn($return);
    }
    // 取消订单
    public function cancel()
    {
        $post['order_no'] = $this->request['order_id'];
        $post['member_id'] = $this->member_id;
        $this->sendInfo('cancelOrder', $post);
        $return['success'] = true;
        $this->ajaxReturn($return);
    }
    // 查询订单
    public function select()
    {
        $post['order_no'] = $this->request['order_id'];
        $post['member_id'] = $this->member_id;
        $data = $this->sendInfo('getOrderDetail', $post);
        $return['success'] = true;
        $info = $data['data'];
        $return['result']['order_id'] = $info['order_no'];
        $return['result']['total_price'] = $info['order_money'];
        $return['result']['return_skus'] = array();
        $return_state = 0;
        switch ($info['order_state']) {
            case '-1':
                $return_state = 5;
                break;
            case '1':
                $return_state = 0;
                break;
            case '2':
                $return_state = 0;
                break;
            case '3':
                $return_state = 1;
                break;
            case '4':
                $return_state = 2;
                break;
            case '5':
                $return_state = 5;
                break;
        }
        $return['result']['state'] = $return_state;
        $return['result']['skus'] = array();
        foreach ($info['cart_info'] as $key => $val) {
            $temp['sku'] = $val['goods_sku'];
            $temp['num'] = $val['goods_num'];
            $temp['price'] = $val['goods_sale_price'];
            array_push($return['result']['skus'], $temp);
        }
        $this->ajaxReturn($return);
    }
    // 物流
    public function track(){
        $post['order_no'] = $this->request['order_id'];
        //$post['order_no'] = $this->request['order_id'];
        $post['member_id'] = $this->member_id;
        $data = $this->sendInfo('getOrderDetail', $post);
        $info = $data['data']['express_info'];
        $return['success'] = true;
        $return['result'] = array();
        if(count($info) == 0){
            $return['desc'] = '还没有发货';
        } else {
            $return['desc'] = '同一订单批量获取物流信息成功';
            foreach ($info as $key=>$val){
                $temp['order_id'] = $val['order_no'];
                $temp['logistic_name'] = $val['express_name'];
                $temp['logistic_no'] = $val['express_no'];
                $temp['shipment_id'] = $val['express_no'];
                $temp['shipment_items'] = array();
                foreach ($val['cart_list'] as $k=>$v){
                    $t1['name'] = $v['goods_info']['goods_name'];
                    $t1['num'] = $v['goods_num'];
                    $t1['sku'] = $v['goods_sku'];
                    array_push($temp['shipment_items'], $t1);
                }
                $temp['track'] = array();
                $t2['content'] = '送货上门';
                $t2['operate_time'] = $v['create_time'];
                $t2['operator'] = '优士创新';
                array_push($temp['track'], $t2);
                array_push($return['result'], $temp);
            }
        }
        
        $this->ajaxReturn($return);
    }
}